$(function () {
    $("#page").load("/html/admin/page.html");
});

/**
 * 查询所有数据
 */

function findAll() {
    let data = {
        name:$("#name").val(),
        houseName:$("#houseName").val(),
        startPrice:$("#startPrice").val(),
        endPrice:$("#endPrice").val(),
        curPage:$("#curPage").val(),
        pageSize:$("#pageSize").val(),
        status:$('input:radio[name="status"]:checked').val()
    };
    let result = myAjax("/admin/product/findAll", data,"post");
    $("#totalCount").html(result.count);
    console.log(result);
    setData(result.list);



}

/**
 * 赋值，将后台的数据在前台显示出来
 * @param data
 */
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += "<tr>\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td>" + clearNull(data[i].houseName) + "</td>\n" +
            "            <td>" + clearNull(data[i].name )+ "</td>\n" +
            "            <td>" + clearNull(data[i].marketPrice + "&nbsp;&nbsp;元/㎡") + "</td>\n" +
            "            <td>" + clearNull(data[i].normalPrice + "&nbsp;&nbsp;元/㎡") + "</td>\n" +
            "            <td>" + clearImg(clearNull(data[i].description)) + "</td>\n" +
            "            <td><img src='" + clearNull(data[i].img) + "' style='width: 30px; height: 30px;' ></td>\n" +
            "            <td>" + (clearNull(data[i].status) == 1 ? "<lable class='normal'>房源充足</lable>" : '<label class="status">售空</lable>') + "</td>\n" +
            "            <td>" + clearNull(data[i].createTime) + "</td>\n" +
            "            <td>" + clearNull(data[i].updateTime) + "</td>\n" +
            "            <td class=\"edit\">\n" +
            "                <div onclick='toEditPage(" + data[i].id + ")'><span class='glyphicon glyphicon-edit' title='编辑'></span></div>\n" +
            "                <div onclick='del(" + data[i].id + ")'><span class='glyphicon glyphicon-trash' title='删除'></span></div>\n" +
            "            </td>\n" +
            "        </tr>";
        // "            <td>" + (clearNull(data[i].isShow) == 1 ? "<lable class='normal'>正常</lable>" : '<label class="abnormal">异常</lable>') + "</td>\n" +
    }
    $("#tbody").html(html);
}

/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}

/**
 * 清除简介中的图片
 * @param value
 * @returns {void | string | never}
 */
function clearImg(value){
    //匹配img标签
    let rule =/<\/?.+?\/?>/g;
    let result = value.replace(rule, '');
    return result ;
}

/***
 * 删除功能
 */
function del(id) {
    let result = myAjax("/admin/product/del", {id: id},"post");
    console.log(id);
    console.log(result);
    if (result.result == 1) {
        //删除成功，就重新查询数据
        findAll();
    } else {
        alert("删除失败！(╥╯^╰╥)");
    }
}

function toEditPage(id) {
    //1.拿到当前产品的id，然后将id传到编辑页面
    // 2.编辑页面加载完成后，需要通过id去数据库查询数据，
    // 3.将查询结果，显示在页面上
    //4.sessionStorage 项目开始时，只能时同一浏览器
    //5.localStorage 项目开始时，可以是不同浏览器
    sessionStorage.setItem("productId", id);
    window.load("/product/productEdit.html");
}